using System;
using System.IO;
using System.Security.Cryptography;
using System.Text;

namespace OracleInternal.Secure.Network
{
	internal class WalletReader
	{
		internal static byte[] field_a = new byte[4]
		{
			161,
			248,
			78,
			54
		};

		internal static byte[] field_b = new byte[4]
		{
			161,
			248,
			78,
			55
		};

		internal static byte[] field_c = new byte[4]
		{
			161,
			248,
			78,
			56
		};

		internal static int d = 4;

		internal static int e = 8;

		internal static int f = 12;

		internal static int g = 13;

		internal static byte h = 6;

		internal static byte i = Convert.ToByte('5');

		internal static int j = 77;

		internal static int k = 8;

		internal static int l = 21;

		internal static int m = 24;

		internal static byte[] n = new byte[8];

		internal static int o = 16;

		internal static int p = g + o;

		internal static int q = 16;

		internal static int r = p + q;

		internal static int s = 16;

		internal static byte[] t = new byte[16]
		{
			192,
			52,
			216,
			49,
			28,
			2,
			206,
			248,
			81,
			240,
			20,
			75,
			129,
			237,
			75,
			242
		};

		internal static byte[] ReadWallet(string WD, ref string WP)
		{
			return a(WD, ref WP);
		}

		internal static byte[] a(string A_0, ref string A_1)
		{
			int num = 0;
			int num2 = 0;
			if (!A_0.EndsWith(Path.DirectorySeparatorChar.ToString() ?? ""))
			{
				A_0 += Path.DirectorySeparatorChar;
			}
			if (!string.IsNullOrEmpty(A_1))
			{
				return a(A_0 + "ewallet.p12");
			}
			byte[] array = a(A_0 + "cwallet.sso");
			if (array.Length < l + m)
			{
				throw new Exception("TCPS: Invalid SSL wallet(length)");
			}
			for (int i = 0; i < 3; i++)
			{
				if (array[i] != WalletReader.field_a[i])
				{
					throw new Exception("TCPS: Invalid SSL Wallet (Magic)");
				}
			}
			if (array[3] != WalletReader.field_a[3] && array[3] != WalletReader.field_b[3])
			{
				throw new Exception("TCPS: Invalid SSL Wallet (magic version)");
			}
			num = (array[d] << 24) + (array[d + 1] << 16) + (array[d + 2] << 8) + array[d + 3];
			num2 = (array[e] << 24) + (array[e + 1] << 16) + (array[e + 2] << 8) + array[e + 3];
			switch (num)
			{
			case 6:
			{
				int num3 = 0;
				byte b = array[f];
				byte[] bytes;
				if (b == WalletReader.i)
				{
					byte[] array2 = new byte[k];
					byte[] array3 = new byte[num2 - 1];
					byte[] array4 = new byte[(num2 - 1) / 2];
					Array.Copy(array, f + 1, array3, 0, array3.Length);
					string @string = Encoding.ASCII.GetString(array3);
					for (int j = 0; j < array4.Length; j++)
					{
						array4[j] = Convert.ToByte(@string.Substring(j * 2, 2), 16);
					}
					Array.Copy(array4, 0, array2, 0, k);
					bytes = new DESCryptoServiceProvider().CreateDecryptor(array2, n).TransformFinalBlock(array4, 8, m);
					num3 = WalletReader.j;
				}
				else
				{
					if (b != h)
					{
						throw new Exception("Invalid Wallet hdr");
					}
					byte[] array5 = new byte[o];
					Array.Copy(array, g, array5, 0, o);
					bytes = new AesCryptoServiceProvider
					{
						Padding = PaddingMode.None,
						Mode = CipherMode.CBC
					}.CreateDecryptor(array5, t).TransformFinalBlock(array, g + o, q);
					num3 = r;
				}
				A_1 = Encoding.Default.GetString(bytes);
				byte[] array6 = new byte[array.Length - num3];
				Array.Copy(array, num3, array6, 0, array.Length - num3);
				return array6;
			}
			case 5:
				return null;
			default:
				throw new Exception("Invalid Wallet hdr version");
			}
		}

		internal static byte[] a(string A_0)
		{
			FileStream fileStream = new FileStream(A_0, FileMode.Open, FileAccess.Read);
			int num = (int)fileStream.Length;
			byte[] array = new byte[num];
			num = fileStream.Read(array, 0, num);
			fileStream.Close();
			return array;
		}
	}
}
